package com.cust.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cust.sky.constant.MessageConstant;
import com.cust.sky.constant.PasswordConstant;
import com.cust.sky.constant.StatusConstant;
import com.cust.sky.context.BaseContext;
import com.cust.sky.dto.EmployeeDTO;
import com.cust.sky.dto.EmployeeLoginDTO;
import com.cust.sky.dto.EmployeePageQueryDTO;
import com.cust.sky.entity.Employee;
import com.cust.sky.exception.AccountLockedException;
import com.cust.sky.exception.AccountNotFoundException;
import com.cust.sky.exception.PasswordErrorException;
import com.cust.sky.mapper.EmployeeMapper;
import com.cust.sky.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 员工信息 服务实现类
 * </p>
 *
 * @author lei
 * @since 2024-07-10
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    /**
     * 员工登录方法
     * 通过员工的用户名和密码进行登录验证，如果验证成功，返回登录的员工信息。
     *
     * @param employeeLoginDTO 包含员工用户名和密码的登录请求DTO（数据传输对象）。
     * @return 登录成功的员工信息。
     */
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        // 从登录请求DTO中获取用户名和密码
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        // 构建查询Wrapper，根据用户名查询员工信息
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, username);
        Employee employee = baseMapper.selectOne(wrapper);

        // 验证员工是否存在，如果不存在，抛出账号不存在异常
        if (employee == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        // 对密码进行MD5加密处理，然后与数据库中保存的密码进行比较
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        // 检查员工账号是否被禁用，如果被禁用，抛出账号禁用异常
        if (employee.getStatus() == StatusConstant.DISABLE) {
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        // 如果验证通过，返回登录的员工信息
        return employee;
    }

    /**
     * 新增员工
     *
     * @param employeeDTO
     */
    @Override
    public void saveEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();

        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO, employee);

        //设置账号的状态，默认正常状态 1表示正常 0表示锁定
        employee.setStatus(StatusConstant.ENABLE);

        //设置密码，默认密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        //设置当前记录创建人id和修改人id
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());
        baseMapper.insert(employee);
    }

    /**
     * 员工条件分页查询
     *
     * @param employeePageQueryDTO
     * @return
     */
    @Override
    public IPage getPage(EmployeePageQueryDTO employeePageQueryDTO) {
        int page = employeePageQueryDTO.getPage();
        int pageSize = employeePageQueryDTO.getPageSize();
        String name = employeePageQueryDTO.getName();
        Page<Employee> pageParam = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper();
        if (StringUtils.hasText(name)) {
            wrapper.like(Employee::getName, name);
        }
        IPage<Employee> employeeIPage = baseMapper.selectPage(pageParam, wrapper);
        return employeeIPage;
    }

    /**
     * 启用或禁用员工账号
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Employee employee = baseMapper.selectById(id);
        employee.setStatus(status);
        employee.setUpdateUser(BaseContext.getCurrentId());
        baseMapper.updateById(employee);
    }

    /**
     * 编辑员工
     *
     * @param employeeDTO
     */
    @Override
    public void updateEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        employee.setUpdateUser(BaseContext.getCurrentId());
        baseMapper.updateById(employee);
    }

}
